const mysql = require('mysql');

const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'root',
  port: '3306',
  database: 'zhr-firstcard'
});

function query (sql, val = false) {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) { reject(err) }
      else {
        connection.query(sql, val, (err, rows) => {
          if (err) { reject(err) }
          else {
            resolve(rows)
            pool.releaseConnection(connection)
          }
        })
      }
    })
  })
}

exports.query = query